<template>
  <div class="page-container">
    <!--面包屑-->
    <div class="breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>会员中心</el-breadcrumb-item>
        <el-breadcrumb-item>会员管理</el-breadcrumb-item>
        <el-breadcrumb-item>查看下级自定义标签</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size">
        <el-row class="search-input">
          <el-row class="times mr30">
            <el-form-item label="创建时间：">
              <el-date-picker
                v-model="filters.startTime"
                type="date"
                placeholder="开始时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
              ></el-date-picker>
            </el-form-item>
            <span class="times-to">-</span>
            <el-form-item>
              <el-date-picker
                v-model="filters.endTime"
                type="date"
                placeholder="结束时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
              ></el-date-picker>
            </el-form-item>
          </el-row>
        </el-row>

        <el-row class="search-input">
          <el-row class="times mr30">
            <el-form-item label="标签名称：" prop="ruleNum">
              <el-input
                v-model="filters.labelName"
                placeholder="输入标签名称"
              ></el-input>
            </el-form-item>
          </el-row>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button icon="el-icon-search" :label="'搜索'" type="primary" @click="findPage(null)" />
          </el-form-item>
          <el-form-item>
            <kt-button :plain="true" :label="'重置'" type="primary" @click="resetForm('filters')" />
          </el-form-item>
          <el-form-item>
            <kt-button :label="'添加标签'" type="primary" @click="handleAdd"/>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
      <!--表格内容栏-->
      <cy-table :height="350"  :columns="filterColumns" @findPage="findPage" 
        :showBatchDelete="false" :showDetailOperation="false" :showPushOperation="false" 
        :showSelection="false"
        :size="size"
        :operationWidth="200"
        :showDeleteOperation="true"
        :showEditOperation="true"
        @handleEdit="handleEdit"
        @handleDelete="handleDeleteInfo"
        ref="CyTable">
      </cy-table>
    <!--新增编辑界面-->
    <el-dialog :title="'添加标签'" :visible.sync="dialogVisible" :close-on-click-modal="false"  @closed="handleClose" class="el-dialog-style">
      <el-form :model="dataForm" :rules="dataFormRules"  ref="dataForm" :size="size" label-position="right">
         <el-form-item class="mb30"> 
           <span class="color999 font16 turnInlineBlock w90 t_r">所属组织：</span>
           <span class="color999 font16">{{dataForm.deptName}}</span>
         </el-form-item>
        <el-form-item  label="标签名称：" prop="labelName">
          <el-input v-model="dataForm.labelName"  auto-complete="off" placeholder="标签名称" style="width:200px" maxlength="20"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size"  @click.native="dialogVisible = false"  type="primary" plain>{{$t('action.cancel')}}</el-button>
        <el-button  :size="size" type="primary"  @click.native="submitForm" :loading="editLoading">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import PopupTreeInput from "@/components/PopupTreeInput";
  import CyTable from "@/views/Core/JicTable";
  import KtButton from "@/views/Core/KtButton";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import { format } from "@/utils/datetime";
  export default {
    components: {
      PopupTreeInput,
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        deptMsg:localStorage.getItem('user'),//当前登录组织信息
        deptType:localStorage.getItem("deptType"),//当前登录用户所属组织类型
        imgUrl: "",
        growthRuleShow: false, //规则创建成功弹窗
        addDeptOperate: true,
        editDeptOperate: false,
        totalSize: 0,
        size: "small",
        filters: {
          start:1,
          limit:10,
          endTime: null,
          startTime: null,
          labelName:null
        },
        columns: [],
        filterColumns: [],
        pageRequest: { pageNum: 1, pageSize: 10 },
        pageResult: [],
        options: {},
        operation: false, // true:新增, false:编辑
        dialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        dataFormRules: {
          labelName: [
            { required: true, message: "请输入标签名称", trigger: "blur" }
          ]
        },
        // 新增编辑界面数据
        dataForm: {
          id: null,
          labelName:'',
          deptName:'',
        }
      };
    },
    methods: {
    // 获取分页数据
      findPage: function(data) {
        let this_=this;
        if(data != null && data.start != null){
        if(data.start != null){
          this.pageRequest.pageNum = data.start;
        }
        if(data.limit != null){
          this.pageRequest.pageSize = data.limit;
        }
       }
        if (this_.filters == undefined || this_.filters == null) {
          this_.filters = {};
        }
        this_.filters.start = this.pageRequest.pageNum;
        this_.filters.limit = this.pageRequest.pageSize;
        this.utils.request.findMemMemberCustomLabelPage(this.filters, function (res) {
          if (res.data == null) {
              res.data = [];
              res.totle = 0;
          }else{
              this_.$refs.CyTable.findPage({content:res.data.rows,total:res.data.total});
          }
        });
      },
    // 获取分页数据
      handleClose() {
        this.$refs.dataForm.resetFields();
        this.dialogVisible = false;
      },

      // 删除
      handleDeleteInfo: function(data) {
          let this_=this;
          var params = {};
          params.id = data.params[0].id;
          this.utils.request.delMemMemberCustomLabel(params, function(res) {
          if (res.code == "0000") {
            this_.dialogVisible = false;
            this_.findPage();
            this_.$message({message: "操作成功",type: "success"});
          } else {
            this_.$message({ message: "操作失败" + res.msg, type: "error"});
          }
        });
         
      },   
      // 显示新增界面
      handleAdd: function() {
        this.dataForm={
          id:null,
          labelName:'',
          deptName:'',
        }
        this.dialogVisible = true;
        this.dataForm.deptName=JSON.parse(this.deptMsg).deptName;
        
      },

      // 显示编辑界面
      handleEdit: function(params) {
        this.addDeptOperate = false;
        this.editDeptOperate = true;
        if (params.row.deptType == "0") {
          this.deptLevel = "母平台";
        } else if (params.row.deptType == "1") {
          this.deptLevel = "子平台";
        } else if (params.row.deptType == "2") {
          this.deptLevel = "商家";
        } else if (params.row.deptType == "3") {
          this.deptLevel = "店铺";
        }
        this.dialogVisible = true;
        this.operation = false;
        this.dataForm = Object.assign({}, params.row);
        this.rangeDis = true;
      },
      // 编辑
      submitForm: function() {
        let this_ = this;
        this.$refs.dataForm.validate(valid => {
          if (valid) {
             let params = Object.assign({}, this_.dataForm);
             this.utils.request.addMemMemberCustomLabel(params, function(res) {
              if (res.code == "0000") {
                this_.dialogVisible = false;
                this_.findPage();
                this_.$message({message: "操作成功",type: "success"});
              } else {
                this_.$message({ message: "操作失败" + res.msg, type: "error"});
              }
               
            });
          }
        });
      },
      //创建成功规则取消
      actionSure() {
        let this_ = this;
        this_.$refs["dataForm"].resetFields();
        this_.growthRuleShow = false;
        this_.dialogVisible = false;
      },
      // 处理表格列过滤显示
      displayFilterColumnsDialog: function() {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function(data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function() {
        this.columns = [
          { prop: "id", label: "ID", minWidth: 100 },
          { prop: "labelName", label: "标签名称", minWidth: 100 },
          { prop: "deptName", label: "所属组织", minWidth: 100 },
          {
            prop: "deptType",
            label: "组织层级",
            formatter: function(row, column, cellValue, index) {
              if (row.deptType == "0") {
                return "母平台";
              } else if (row.deptType == "1") {
                return "子平台";
              } else if (row.deptType == "2") {
                return "商家";
              } else if (row.deptType == "3") {
                return "店铺";
              }
            }
          },
          { prop: "createTime", label: "创建时间", minWidth: 120 },
          { prop: "markTimes", label: "标记次数", minWidth: 120 },
        ];
        var temp = [];
        $.each(this.columns, function(key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      resetForm(formName) {
        //需要校验的key
        let keys = ["startTime","endTime","labelName"]
        let boo = this.seachTableUtils.checkFromIsNull(this.filters,keys)
        if(!boo){
          return
        }
        this.filters = {
          start:1,
          limit:10,
          endTime: null,
          startTime: null,
          labelName:null
        };
        this.findPage();
      },
    },
    mounted() {
      this.initColumns();
      this.findPage();
    }
  };
</script>

<style lang='scss' scoped>
  .dialogInput {
    text-align: left;
    /deep/ .el-input {
      width: 200px;
    }
    /deep/ .el-input__inner {
      width: 200px !important;
    }
  }
  .w90{
    width: 90px;
  }
  .search-input{
  .times {
    .times-to {
      padding: 0 10px;
      height: 32px;
      line-height: 32px;
      color: #999;
      display: inline-block;
    }
    /deep/ .el-input{
      width:auto !important;
      /deep/ .el-input__inner{
        width: auto !important;
      }
    }
  }
}
</style>
